package com.softeem.music_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.PlaylistMusic;
import com.softeem.music_sys.service.PlaylistMusicService;
import com.softeem.music_sys.mapper.PlaylistMusicMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author RD
* @description 针对表【playlist_music(歌单包含的音乐关系表)】的数据库操作Service实现
* @createDate 2025-11-07 11:19:17
*/
@Service
public class PlaylistMusicServiceImpl extends ServiceImpl<PlaylistMusicMapper, PlaylistMusic>
    implements PlaylistMusicService{

    @Override
    public Result addMusicToPlaylist(PlaylistMusic playlistMusic) {
        // 检查是否已经添加
        QueryWrapper<PlaylistMusic> wrapper = new QueryWrapper<>();
        wrapper.eq("playlist_id", playlistMusic.getPlaylistId());
        wrapper.eq("music_id", playlistMusic.getMusicId());
        PlaylistMusic exist = this.getOne(wrapper);
        if (exist != null) {
            return Result.fail("音乐已在歌单中");
        }
        
        // 设置添加时间
        playlistMusic.setAddTime(new Date());
        
        boolean result = this.save(playlistMusic);
        return result ? Result.success().setMessage("添加成功") : Result.fail("添加失败");
    }

    @Override
    public Result removeMusicFromPlaylist(Integer playlistId, Integer musicId) {
        QueryWrapper<PlaylistMusic> wrapper = new QueryWrapper<>();
        wrapper.eq("playlist_id", playlistId);
        wrapper.eq("music_id", musicId);
        boolean result = remove(wrapper);
        return result ? Result.success().setMessage("移除成功") : Result.fail("移除失败");
    }

    @Override
    public Result deletePlaylistMusic(Integer id) {
        PlaylistMusic playlistMusic = this.getById(id);
        if (playlistMusic == null) {
            return Result.fail("记录不存在");
        }
        boolean result = this.removeById(id);
        return result ? Result.success().setMessage("删除成功") : Result.fail("删除失败");
    }

    @Override
    public Page<PlaylistMusic> getMusicsByPlaylist(Integer playlistId, Integer page, Integer limit) {
        Page<PlaylistMusic> pageUtil = new Page<>(page, limit);
        QueryWrapper<PlaylistMusic> wrapper = new QueryWrapper<>();
        wrapper.eq("playlist_id", playlistId);
        wrapper.orderByDesc("add_time");
        return page(pageUtil, wrapper);
    }

    @Override
    public Page<PlaylistMusic> getPlaylistsByMusic(Integer musicId, Integer page, Integer limit) {
        Page<PlaylistMusic> pageUtil = new Page<>(page, limit);
        QueryWrapper<PlaylistMusic> wrapper = new QueryWrapper<>();
        wrapper.eq("music_id", musicId);
        wrapper.orderByDesc("add_time");
        return page(pageUtil, wrapper);
    }

    @Override
    public Result batchAddMusicToPlaylist(Integer playlistId, List<Integer> musicIds) {
        if (musicIds == null || musicIds.isEmpty()) {
            return Result.fail("音乐列表不能为空");
        }
        
        int successCount = 0;
        for (Integer musicId : musicIds) {
            // 检查是否已经添加
            QueryWrapper<PlaylistMusic> wrapper = new QueryWrapper<>();
            wrapper.eq("playlist_id", playlistId);
            wrapper.eq("music_id", musicId);
            PlaylistMusic exist = this.getOne(wrapper);
            if (exist != null) {
                continue;
            }
            
            PlaylistMusic playlistMusic = new PlaylistMusic();
            playlistMusic.setPlaylistId(playlistId);
            playlistMusic.setMusicId(musicId);
            playlistMusic.setAddTime(new Date());
            
            if (this.save(playlistMusic)) {
                successCount++;
            }
        }
        
        return Result.success().setMessage("成功添加" + successCount + "首音乐");
    }

    @Override
    public Result clearPlaylist(Integer playlistId) {
        QueryWrapper<PlaylistMusic> wrapper = new QueryWrapper<>();
        wrapper.eq("playlist_id", playlistId);
        boolean result = remove(wrapper);
        return result ? Result.success().setMessage("清空成功") : Result.fail("清空失败");
    }

    @Override
    public boolean isMusicInPlaylist(Integer playlistId, Integer musicId) {
        QueryWrapper<PlaylistMusic> wrapper = new QueryWrapper<>();
        wrapper.eq("playlist_id", playlistId);
        wrapper.eq("music_id", musicId);
        return count(wrapper) > 0;
    }

    @Override
    public long countMusicInPlaylist(Integer playlistId) {
        QueryWrapper<PlaylistMusic> wrapper = new QueryWrapper<>();
        wrapper.eq("playlist_id", playlistId);
        return count(wrapper);
    }
}